/*
 * Copyright 2010 Spolecne s.r.o. (www.spoledge.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.appengine.api.datastore;

import java.io.Serializable;

/**
 * This is a GWT emulation class.
 * It is ONLY used on the client side - JavaScript.
 *
 * This class is intended to use with custom DTO serializers
 * generated by AuDAO.
 *
 * Method hashCode() can differ from the original one.
 */
public class GeoPt implements Serializable, Comparable<GeoPt> {

    public static final long serialVersionUID = 349808987517153697L;

    private float latitude;
    private float longitude;


    ////////////////////////////////////////////////////////////////////////////
    // Constructors
    ////////////////////////////////////////////////////////////////////////////

    public GeoPt(float latitude, float longitude) {
        this.latitude = latitude;
        this.longitude = longitude;
    }


    @SuppressWarnings("unused")
    private GeoPt() {
    }


    ////////////////////////////////////////////////////////////////////////////
    // Public
    ////////////////////////////////////////////////////////////////////////////

    public float getLatitude() {
        return latitude;
    }


    public float getLongitude() {
        return longitude;
    }


    ////////////////////////////////////////////////////////////////////////////
    // Comparable
    ////////////////////////////////////////////////////////////////////////////

    public int compareTo( GeoPt o ) {
        int lat = Float.valueOf( latitude ).compareTo( Float.valueOf( o.latitude ));
        return lat != 0 ? lat : Float.valueOf( longitude ).compareTo( Float.valueOf( o.longitude ));
    }


    ////////////////////////////////////////////////////////////////////////////
    // Misc
    ////////////////////////////////////////////////////////////////////////////

    public boolean equals( Object o ) {
        if (this == o) return true;
        if (o == null || (!(o instanceof GeoPt))) return false;

        return compareTo( (GeoPt) o) == 0;
    }


    public int hashCode() {
        // Float.floatToIntBits is not supported by GWT:
        // return 29 * Float.floatToIntBits( latitude ) + Float.floatToIntBits( longitude );
        return 29 * (int)( 100f * latitude ) + (int)( 100f * longitude );
    }


    public String toString() {
        return "[" + latitude + ", " + longitude + ']';
    }
}
